package cn.idongjia.kuaidi.util;

import cn.idongjia.kuaidi.Const;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;


/**
 * author:lijian
 */
public class HttpConnectionManager {

    private static PoolingHttpClientConnectionManager connMgr;
    private static RequestConfig requestConfig;


    /**
     * 初始化http连接池，设置参数、http头等等信息
     */
    static  {
// 设置连接池
        connMgr = new PoolingHttpClientConnectionManager();
        connMgr.setMaxTotal(Const.SUM_60);
        connMgr.setDefaultMaxPerRoute(Const.SUM_30);

        // 设置超时时间1s
        RequestConfig.Builder configBuilder = RequestConfig.custom();
        configBuilder.setSocketTimeout(Const.SUM_60000);
        configBuilder.setConnectTimeout(Const.SUM_60000);
        configBuilder.setConnectionRequestTimeout(Const.SUM_60000);
        requestConfig = configBuilder.build();

    }

    public static HttpClient getHttpClient() {
        return HttpClients.custom().setConnectionManager(connMgr).setDefaultRequestConfig(
                requestConfig).build();
    }
}